// pages/Affordable/index.js
var common = require("../../public.js");
Page({
  data: {
    userInfo: '',
    token: '',
    openid: '',
    pageNum: 1,
    xpoint: '',
    ypoint: '',
    cityName: '枣庄市',
    regionName: '薛城区',
    goodsList: [],
    showDesc: false,
    showPrice: false,
    showSell: false,
    sellValue: '',
    priceValue: '',
    descValue: '',
    option1: [
      { text: '默认', value: '' },
      { text: '由远到近', value: 1 },
      { text: '由近到远', value: 2 },
    ],
    option2: [
      { text: '默认', value: ' ' },
      { text: '由高到低', value: 1 },
      { text: '由低到高', value: 2 },
    ],
    option3: [
      { text: '默认', value: ' ' },
      { text: '由高到低', value: 1 },
      { text: '由低到高', value: 2 },
    ],
    isNoMoreData: false, // 是否加载完成
    refreshing: false
  },

  onLoad: function (options) {
    let that = this
    getApp().getLocation(function (data) {
      wx.getStorage({
        key: 'user',
        success: function (res) {
          that.setData({
            userInfo: res.data.user,
            token: res.data.token,
            openid: res.data.openid,
          })
        }
      })
      that.setData({
        xpoint: data.longitude,
        ypoint: data.latitude,
        cityName: data.regeocodeData.addressComponent.city,
        regionName: data.regeocodeData.addressComponent.district
      }, function () {
        that.getStoreItemListNew()
      })
    })
  },
  getStoreItemListNew(refresh = true) {
    let pageNum = 1
    const that = this
    if (refresh) {
      this.setData({
        isNoMoreData: false
      })
      pageNum = 1
    } else {
      if (this.data.isNoMoreData) {
        return
      }
      pageNum = that.data.pageNum + 1
    }
    this.setData({
      refreshing: true
    })
    let tempJson = {
      pageNum,
      xpoint: that.data.xpoint,
      ypoint: that.data.ypoint,
      cityName: that.data.cityName,
      regionName: that.data.regionName,
      distianceSort: that.data.descValue,
      priceSort: that.data.priceValue,
      saleNumSort: that.data.sellValue,
    }
    common.post("/getStoreItemListNew", tempJson, function (res) {
      if (res.status === 200) {
        that.data.pageNum = pageNum
        if (refresh) {
          that.setData({
            goodsList: res.data.result_list,
          })
        } else {
          if (res.data.result_list == '') {
            that.setData({
              isNoMoreData: true
            })
          } else {
            that.setData({
              goodsList: [...that.data.goodsList, ...res.data.result_list]
            })
          }
        }
      }
    }, function () {
      if (refresh) {
        that.setData({
          refreshing: false
        })
      }
    })
  },
  backPage() {
    wx.navigateBack({
      delta: 1
    })
  },
  buyNow(e) {
    common.gotoGoodsPage(e.currentTarget.dataset.item)
  },
  clickDesc() {
    this.setData({
      showDesc: true,
      showPrice: false,
      showSell: false
    })
  },
  clickPrice() {
    this.setData({
      showPrice: true,
      showDesc: false,
      showSell: false
    })
  },
  clickPopItem(e) {
    console.log(e.currentTarget.dataset)
    let that = this
    let {
      index,
      type
    } = e.currentTarget.dataset
    switch (type) {
      case 'sell':
        this.setData({
          sellValue: this.data.option3[index].value,
          priceValue: '',
          descValue: '',
        }, _ => {
          that.getStoreItemListNew()
        })
        break;
      case 'price':
        this.setData({
          priceValue: this.data.option2[index].value,
          sellValue: '',
          descValue: '',
        }, _ => {
          that.getStoreItemListNew()
        })

        break;
      case 'desc':
        this.setData({
          descValue: this.data.option1[index].value,
          priceValue: '',
          sellValue: '',
        }, _ => {
          that.getStoreItemListNew()
        })
        break;
      default:
        break;
    }

    this.hiddenPop()
  },
  clickSold() {
    this.setData({
      showSell: true,
      showPrice: false,
      showDesc: false,
    })
  },
  hiddenPop() {
    this.setData({
      showSell: false,
      showPrice: false,
      showDesc: false,
    })
  },
  onReachBottom() {
    this.getStoreItemListNew(false)
  },
  onShareAppMessage: function () {
    return {
      title: '飞羊精选',
      path: 'packageA/pages/Affordable/index',
         imageUrl: 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/xfj/share.jpg',
      success: (res) => { },
      fail: (res) => {
        // 分享失败
      }
    }
  },
  onShareTimeline(){
    return {
      title: '飞羊精选',
      path: 'packageA/pages/Affordable/index',
         imageUrl: 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/xfj/share.jpg',
      success: (res) => { },
      fail: (res) => {
        // 分享失败
      }
    }
  }
})